/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.hohai.river.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hohai.river.utils.Node;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.hohai.river.entity.SysRegion;
import com.hohai.river.service.SysRegionService;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 地区表
 *
 * @author pig code generator
 * @date 2022-09-14 14:42:30
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/sysregion")
@Tag(name = "地区表管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class SysRegionController {

	private final SysRegionService sysRegionService;

	/**
	 * 分页查询
	 * @param page 分页对象
	 * @param sysRegion 地区表
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('river_sysregion_get')")
	public R getSysRegionPage(Page page, SysRegion sysRegion) {
		return R.ok(sysRegionService.page(page, Wrappers.query(sysRegion)));
	}

	/**
	 * 通过id查询地区表
	 * @param regionId id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{regionId}")
	@PreAuthorize("@pms.hasPermission('river_sysregion_get')")
	public R getById(@PathVariable("regionId") String regionId) {
		return R.ok(sysRegionService.getById(regionId));
	}

	/**
	 * 新增地区表
	 * @param sysRegion 地区表
	 * @return R
	 */
	@Operation(summary = "新增地区表", description = "新增地区表")
	@SysLog("新增地区表")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('river_sysregion_add')")
	public R save(@RequestBody SysRegion sysRegion) {
		return R.ok(sysRegionService.save(sysRegion));
	}

	/**
	 * 修改地区表
	 * @param sysRegion 地区表
	 * @return R
	 */
	@Operation(summary = "修改地区表", description = "修改地区表")
	@SysLog("修改地区表")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('river_sysregion_edit')")
	public R updateById(@RequestBody SysRegion sysRegion) {
		return R.ok(sysRegionService.updateById(sysRegion));
	}

	/**
	 * 通过id删除地区表
	 * @param regionId id
	 * @return R
	 */
	@Operation(summary = "通过id删除地区表", description = "通过id删除地区表")
	@SysLog("通过id删除地区表")
	@DeleteMapping("/{regionId}")
	@PreAuthorize("@pms.hasPermission('river_sysregion_del')")
	public R removeById(@PathVariable String regionId) {
		return R.ok(sysRegionService.removeById(regionId));
	}

	/**
	 * 获取级联下拉选择框列表
	 * @param pid id
	 * @return R
	 */
	@Operation(summary = "获取级联下拉选择框列表", description = "获取级联下拉选择框列表")
	@SysLog("获取级联下拉选择框列表")
	@GetMapping("/cascade/{pid}")
	@PreAuthorize("@pms.hasPermission('river_sysregion_get')")
	public R getCascadeData(@PathVariable Long pid, @RequestParam(value = "level", required = false) Integer level,
			@RequestParam(value = "year", required = false) String year) {
		return R.ok(sysRegionService.getCascadeData(pid, level, year));
	}

	/**
	 * 获取下拉选择框列表
	 * @param pid id
	 * @return R
	 */
	@Operation(summary = "获取下拉选择框列表", description = "获取下拉选择框列表")
	@SysLog("获取下拉选择框列表")
	@GetMapping("/select/{pid}")
	@PreAuthorize("@pms.hasPermission('river_sysregion_get')")
	public R getSelectList(@PathVariable Long pid, @RequestParam(value = "all", required = false) Boolean all) {
		List<Node> selectList = sysRegionService.getSelectList(pid);
		if (all != null && all) {
			selectList.add(0, new Node(-1L, "所有城市"));
		}
		return R.ok(selectList);
	}

}
